<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="审批类型" prop="workFlowName">
        <el-input
          v-model="queryParams.workFlowName"
          placeholder="请输入审批类型"
          clearable
          :style="{ width: '220px' }"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="当事人名称" prop="partyName">
        <el-input
          v-model="queryParams.partyName"
          placeholder="请输入当事人名称"
          clearable
          :style="{ width: '220px' }"
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb10">
      <right-toolbar v-model:showSearch="showSearch" :columns="columns" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      border
      :data="partyinfoList"
      show-overflow-tooltip
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="column in newColumns" :key="column.key">
        <el-table-column
          :label="column.label"
          :prop="column.key"
          :min-width="column.minWidth || ''"
          :width="column.width || ''">
          <template #default="scope" v-if="column.key === 'status'">
            <el-tag type="primary" v-if="scope.row.status === '1'">审批中</el-tag>
            <el-tag type="danger" v-else-if="scope.row.status === '2'">已退回</el-tag>
            <el-tag type="danger" v-else-if="scope.row.status === '3'">不予立案</el-tag>
            <el-tag type="info" v-else-if="scope.row.status === '4'">已撤案</el-tag>
            <el-tag type="success" v-else-if="scope.row.status === '5'">已完成</el-tag>
            <el-tag type="danger" v-else-if="scope.row.status === '6'">已终止</el-tag>
          </template>
        </el-table-column>
      </template>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="180px"
        fixed="right"
        :show-overflow-tooltip="false">
        <template #default="scope">
          <el-button v-if="scope.row.status === '2'" link type="primary" @click="handleRefresh(scope.row)">
            <el-icon><refresh /></el-icon>
            重新提交
          </el-button>
          <el-button v-if="scope.row.status !== '2'" link type="primary" icon="Edit" @click="handleApprove(scope.row)">
            审批
          </el-button>
          <el-button link type="primary" @click="handleFlowRecord(scope.row)">
            <svg-icon icon-class="liuzhuanjilu" />
            流转记录
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList" />
    <flow-record v-model:modelValue="circulationVisible" :flow-list="circulationList" />
  </div>
</template>

<script setup name="Partyinfo">
import { getTodoList, getTodoFlowRecord } from '@/api/workFlow/index'
import { useRouter } from 'vue-router'
import flowRecord from '@/components/flowRecord/index.vue'

const { proxy } = getCurrentInstance()
const router = useRouter()
const partyinfoList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const formId = ref('')
const title = ref('')

const columns = ref([
  { key: 'workFlowName', label: '审批类型', visible: true, minWidth: '135' },
  { key: 'partyName', label: '当事人名称', visible: true, minWidth: '175' },
  { key: 'cause', label: '案由', visible: true, minWidth: '220' },
  { key: 'deptName', label: '执法队', visible: true, minWidth: '150' },
  { key: 'lawPersion', label: '执法人员', visible: true, minWidth: '200' },
  { key: 'status', label: '审批状态', visible: true, minWidth: '120' },
  { key: 'applyTime', label: '申请时间', visible: true, width: '160' },
])
const newColumns = computed(() => {
  return columns.value.filter(column => column.visible)
})

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    partyName: null,
    createBy: null,
    createTime: null,
  },
})

const { queryParams } = toRefs(data)

/** 查询当事人信息列表 */
function getList() {
  loading.value = true
  queryParams.value.params = {}
  getTodoList(queryParams.value).then(response => {
    partyinfoList.value = response.rows
    total.value = response.total || 0
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 重新提交 */
function handleRefresh(row) {
  formId.value = row.formId
  title.value = '修改当事人信息'
  router.push({
    path: '/workbench/workFlow/detail',
    query: {
      id: formId.value,
      workFlowId: row.workFlowId,
      taskId: row.taskId,
      instanceId: row.instanceId,
      type: 'refresh',
    },
  })
}

/** 审批按钮操作 */
function handleApprove(row) {
  formId.value = row.formId
  title.value = '修改当事人信息'
  router.push({
    path: '/workbench/workFlow/detail',
    query: {
      id: formId.value,
      workFlowId: row.workFlowId,
      taskId: row.taskId,
      instanceId: row.instanceId,
      type: 'edit',
    },
  })
}

const circulationList = ref([])
const circulationVisible = ref(false)

/** 流转记录按钮操作 */
async function handleFlowRecord(row) {
  const res = await getTodoFlowRecord({ instanceId: row.instanceId, order: '1' })
  const { data } = res
  circulationList.value = data
  circulationVisible.value = true
}

getList()
</script>
